package cc.javaee.bbs.controller.index;

import cc.javaee.bbs.model.*;
import cc.javaee.bbs.service.*;
import cc.javaee.bbs.tool.PublicStatic;
import cc.javaee.bbs.tool.Tool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/*
 * 板块的查询
 * 
 */
@Controller
@RequestMapping("/bk")
public class IndexBankuaiController {
	@Autowired
	BankuaiService bankuaiService;

	@Autowired
	TieziService tieziService;

	@Autowired
	ZhutiService zhutiService;

	@Autowired
	LinkService linkService;

	@Autowired
	UserService userService;

	private static Logger log = LoggerFactory.getLogger(IndexBankuaiController.class);
				//,produces = "application/json"
	//首页查询
	@RequestMapping(value = "index.do")
	public String findAllWhere(HttpServletRequest request, Model model, String bankuaiid, Integer zhutiid, PageBean<Tiezi> page, String orderby) {


//		long currentTimeMillis = System.currentTimeMillis();
		//查询当前板块
		Bankuai bankuai = bankuaiService.findbyid(bankuaiid);

		//判断访问权限
		User user = (User) request.getSession().getAttribute(PublicStatic.USER);
		boolean flag = userService.isfind(bankuai.getFindgroup(), user);
		if (!flag) {
			return "redirect:/topage/quanxian.do";
		}


		//查询主题列表
		Zhuti zhuti = new Zhuti();
		zhuti.setBankuaiId(Integer.parseInt(bankuaiid));
		List<Zhuti> zhutilist = zhutiService.find(zhuti);
		//查询板块的帖子
		Tiezi tiezi = new Tiezi();
		tiezi.setBankuaiId(Integer.parseInt(bankuaiid));
		tiezi.setOrderby(orderby);
		if (zhutiid != null && !zhutiid.equals("0")) {
			tiezi.setZhutiid(zhutiid);
		} else {
			zhutiid = 0;
		}
		//查询列表
		page = tieziService.findpagebybankuanid(tiezi, page);
		//今日的帖子总数
		int nowcount = tieziService.findnowcount(bankuaiid);
		//查询置顶
		List<Tiezi> zhidinglist = tieziService.findzhiding(bankuaiid);
//		System.out.println(System.currentTimeMillis()-currentTimeMillis);

		//查询友情链接
		Link link = new Link();
		link.setIsshow("1");
		link.setBankuaiid(Integer.parseInt(bankuaiid));
		List<Link> linklist = linkService.find(link);
		model.addAttribute("linklist", linklist);

		model.addAttribute("page", page);
		model.addAttribute("bankuaiid", bankuaiid);
		model.addAttribute("zhutiid", zhutiid);
		model.addAttribute("orderby", orderby);
		model.addAttribute("bankuai", bankuai);
		model.addAttribute("zhutilist", zhutilist);
		model.addAttribute("nowcount", nowcount);
		model.addAttribute("zhidinglist", zhidinglist);

		if (Tool.ismobile(request)) {
			return "jsp/mobile/bankuai";
		} else {
			return "jsp/index/bankuai";
		}
	}

	//首页查询
	@RequestMapping("save_add.do")
	public String addBankuai(HttpServletRequest request, Model model, Bankuai bankuai) {

		bankuaiService.insert(bankuai);
		return null;
	}

	//首页查询
	@RequestMapping("page_add.do")
	public String addBankuaiPage() {

		return "jsp/index/addbankuai";
	}
}